const http = require("../../utils/request")
const isEmpty = (obj)=>{
  try {
    if (obj == null || obj == undefined) {
      return true;
    }
    //判断数字是否是NaN
    if (typeof obj === "number") {
      if (isNaN(obj)) {
        return true;
      } else {
        return false;
      }
    }
    //判断参数是否是布尔、函数、日期、正则，是则返回false
    if (typeof obj === "boolean" || typeof obj === "function" || obj instanceof Date ||
      obj instanceof RegExp) {
      return false;
    }
    //判断参数是否是字符串，去空，如果长度为0则返回true
    if (typeof obj === "string") {
      if (obj.trim().length == 0) {
        return true;
      } else {
        return false;
      }
    }
    if (typeof obj === 'object') {
      //判断参数是否是数组，数组为空则返回true
      if (obj instanceof Array) {
        if (obj.length == 0) {
          return true;
        } else {
          return false;
        }
      }
      //判断参数是否是对象，判断是否是空对象，是则返回true
      if (obj instanceof Object) {
        //判断对象属性个数
        if (Object.getOwnPropertyNames(obj).length == 0) {
          return true;
        } else {
          return false;
        }
      }
    }
  } catch (e) {
    return false;
  }
}
const getCultivation = (page,size,state,code)=>{
    let statement;let pages;
    let codeing;let sizes;
    if(isEmpty(state)){
      statement = "";
    }else{
      statement = `?state=${state}`;
    }
    if(isEmpty(code)){
      codeing = "";
    }else{
      codeing = `?code=${code}`;
    }
    if(isEmpty(page)){
      pages = ""
    }else{
      pages = `/${page}`;
    }
    if(isEmpty(size)){
      sizes = ""
    }else{
      sizes = `/${size}`;
    }
    return new Promise((resolve,error)=>{
      http.getRequest("/admin/cultivation"+pages+sizes+statement+codeing
      ,{}
      ,res => {
        let data = [];
        //处理数据
        if(!res.data.data.records&&Object.prototype.toString.call(res.data.data) === '[object Object]'){
          data = res.data.data;
        }else if(res.data.data&&res.data.data.records.length>0){
          for (let i = 0; i < res.data.data.records.length; i++) {
            let d = res.data.data.records[i];
            if ( d["picture"]!==null && d["picture"].length >= 1) {
              d["pic"] = d["picture"][0]
            }else{
              d["pic"] = "https://traceability-1258993064.cos.ap-guangzhou.myqcloud.com/475428814310060032.png"
            }
            data.push(d);
          }
       }
       console.log('data',data);
        resolve(data);
      },err =>{
        error(err);
      })
    })
}
const getCulDispath = () =>{

}
export{
  getCultivation,
  getCulDispath
}